﻿using BookManege.service;


namespace BookManege.controller
{
    public partial class borrowBook : Form
    {
        private BookManegeService bookManegeService;
        private bookBorrowService bookBorrowService;
        private int pageNum;
        private int pageSize;
        public borrowBook()
        {
            InitializeComponent();
            this.pageNum = 1;
            this.pageSize = 10;
            this.bookBorrowService = new bookBorrowService();
            this.bookManegeService = new BookManegeService();
        }
        private void borrowBook_Load(object sender, EventArgs e)
        {
            GetBookList();
            if (pageNum == 1)
            {
                linkLabel1.Enabled = false;
            }
            if (bookManegeService.GetCount() == pageNum * pageSize)
            {
                linkLabel2.Enabled = false;
            }
            textBox3.Text = pageNum.ToString();
        }
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.Columns[e.ColumnIndex].Name == "借阅此书")
            {
                long userId = Program.user.Id;
                long bookId = (long)dataGridView1.Rows[e.RowIndex].Cells["id"].Value;
                int bookCount = (int)dataGridView1.Rows[e.RowIndex].Cells["数量"].Value;
                string bookName = dataGridView1.Rows[e.RowIndex].Cells["书名"].Value.ToString();
                string auther = dataGridView1.Rows[e.RowIndex].Cells["作者"].Value.ToString();
                //判断是否已经借阅过了
                if (!bookBorrowService.isBorrow(userId, bookId))
                {
                    if (MessageBox.Show("确认要借阅《" + dataGridView1.Rows[e.RowIndex].Cells["书名"].Value.ToString() + "》吗", "确认", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        new borrowBookInfo(userId, bookId, bookCount, bookName, auther).Show();
                    }
                }
                else
                {
                    if (MessageBox.Show("您的清单已经有《" + dataGridView1.Rows[e.RowIndex].Cells["书名"].Value.ToString() + "》了，还要增加吗？", "确认", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        new borrowBookInfo(userId, bookId, bookCount, bookName, auther).Show();
                    }
                }
                GetBookList();
            }
        }
        //总书本数
        public int GetCount()
        {
            return bookManegeService.GetCount();
        }
        private void GetBookList()
        {
            dataGridView1.DataSource = bookManegeService.GetBookListByUser(pageNum, pageSize);
        }
        //搜索
        private void button1_Click(object sender, EventArgs e)
        {
            string bookName = textBox1.Text;
            string bookAuther = textBox2.Text;
            dataGridView1.DataSource = bookManegeService.search(bookName, bookAuther);
        }
        //重置
        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox2.Text = string.Empty;
            GetBookList();
        }
        //上一页
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (pageNum == 1)
            {
                linkLabel1.Enabled = false;
            }
            else
            {
                linkLabel2.Enabled = true;
                --pageNum;
                textBox3.Text = pageNum.ToString();
                GetBookList();
            }
        }
        //下一页
        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (bookManegeService.GetCount() - pageNum * pageSize < 1)
            {
                linkLabel2.Enabled = false;
            }
            else
            {
                linkLabel1.Enabled = true;
                ++pageNum;
                textBox3.Text = pageNum.ToString();
                GetBookList();
            }
        }
        //页码跳转
        private void button3_Click(object sender, EventArgs e)
        {
            pageNum = int.Parse(textBox3.Text);
            GetBookList();
        }
        //每页行数
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            pageNum = 1;
            pageSize = (comboBox2.SelectedIndex + 1) * 10;
            GetBookList();
        }
    }
}
